package com.example.demo.service.impl;

import com.example.demo.domain.City;
import com.example.demo.domain.CitySpotCountAndRating;
import com.example.demo.domain.CitySpotNum;
import com.example.demo.domain.DTO.CityHot;
import com.example.demo.domain.ProvinceCitySpotInfo;
import com.example.demo.service.CityService;
import com.example.demo.mapper.CityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author z'z'w
 * @description 针对表【CITY】的数据库操作Service实现
 * @createDate 2023-05-07 14:59:21
 */
@Service
public class CityServiceImpl implements CityService {

    @Autowired
    private CityMapper cityMapper;

    @Override
    public List<City> selectAll() {
        return cityMapper.selectAll();
    }

    @Override
    public HashMap<String, HashMap<String, Float>> selectCityHot() {
        List<CityHot> cityHots = cityMapper.selectCityHot();
//        热门城市(x为省份 y为城市 z为热度值)

        //存储为map
        HashMap<String, HashMap<String, Float>> provinceCityHotMap = new HashMap<>();
        for (CityHot cityHot : cityHots) {
            Float hot = (Float) cityHot.getHot();
            if (hot == 0) continue;
            String provinceName = cityHot.getProvinceName();
            if (provinceName == null || provinceName.equals("")) continue;
            String cityName = cityHot.getCityName();
            if (cityName == null || cityName.equals("")) continue;
            //将值聚合在map当中
            //如果省份key已经存在
            if (provinceCityHotMap.containsKey(provinceName)) {
                //拿取该省份的城市热度map
                HashMap<String, Float> cityHotMap = provinceCityHotMap.get(provinceName);
                //如果这个城市已经存在这个省份下，那就不处理，如果没有在该省份下，需要为其添加城市及城市热度
                if (!cityHotMap.containsKey(cityName)) {
                    cityHotMap.put(cityName, hot);
                }
            } else {
                //如果省份key不存在
                //新建一个其城市热度的map
                HashMap<String, Float> cityHotMap = new HashMap<>();
                cityHotMap.put(cityName, hot);
                //加入到省份结果map中
                provinceCityHotMap.put(provinceName, cityHotMap);
            }
        }
        return provinceCityHotMap;
    }

    @Override
    public HashMap<String, HashMap<String, List<String>>> selectProvinceCitySpot() {

        HashMap<String, HashMap<String, List<String>>> resultMap = new HashMap<>();
        List<ProvinceCitySpotInfo> provinceCitySpotInfos = cityMapper.selectProvinceCitySpot();
        for (ProvinceCitySpotInfo provinceCitySpotInfo : provinceCitySpotInfos) {
            //景点名
            String spotName = provinceCitySpotInfo.getSpotName().replace("AAAA景区", "").replaceAll("\\s*|\t|\r|\n", "");
            //城市名
            String cityName = provinceCitySpotInfo.getCityName();
            //省份名
            String provinceName = provinceCitySpotInfo.getProvinceName();

            //第一种情况-省份
            //没有这个省份
            if (!resultMap.containsKey(provinceName)) {
                //封装城市和景点
                HashMap<String, List<String>> cityMap = new HashMap<>();
                List<String> spotList = new ArrayList<>();
                spotList.add(spotName);
                cityMap.put(cityName, spotList);
                resultMap.put(provinceName, cityMap);

            }
            //如果这个省份已经存在
            else {
                //拿到这个省份城市map
                HashMap<String, List<String>> cityMap = resultMap.get(provinceName);
                //两种情况，一种城市已存在，另一种城市不存在
                //城市存在
                if (cityMap.containsKey(cityName)) {
                    //拿取并封装
                    List<String> spotList = cityMap.get(cityName);
                    spotList.add(spotName);
                }
                //城市不存在
                else {
                    //新建并封装
                    List<String> spotList = new ArrayList<>();
                    spotList.add(spotName);
                    cityMap.put(cityName, spotList);
                }
            }
        }
        return resultMap;
    }

    @Override
    public List<CitySpotNum> selectCitySpotNum() {
        return cityMapper.selectCitySpotNum();
    }

    @Override
    public List<CitySpotCountAndRating> selectCitySpotAvgRating() {
        return cityMapper.selectCitySpotAvgRating();
    }
}




